﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVT.Entity;
using QLVT.DAO;

namespace QLVT.UI
{
    public partial class UcKhachHang : UserControl
    {
        bool add = false;

        public UcKhachHang()
        {
            InitializeComponent();
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            if (!add)
            {
                toolStripButtonOk.Visible = true;
                add = true;
                toolStripButtonAdd.Visible = false;
                txtMaNV.Text = null;
                txtHoLot.Text = null;
                txtTenNV.Text = null;
                txtSDT.Text = null;
                dateTimePickerNgaySinh.Value = DateTime.Today;
                txtSCMND.Text = null;
            }

            
        }

        public void BinddingData(KhachHang khachHang)
        {
            txtMaNV.Text = khachHang.Makh;
            txtHoLot.Text = khachHang.Holot;
            txtTenNV.Text = khachHang.Tenkh;
            txtSDT.Text = khachHang.Sdt;
            dateTimePickerNgaySinh.Value = khachHang.Ngaysinh;
            txtSCMND.Text = khachHang.Socmnd;
        }

        private void dataGridViewKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow rowSelect = dataGridViewKhachHang.SelectedRows[0];
            KhachHangDAO KhachHangDao = new KhachHangDAO();
            KhachHang KhachHang = KhachHangDao.GetById(rowSelect.Cells[0].Value.ToString());
            if (KhachHang != null)
            {
                BinddingData(KhachHang);
            }
            if (add)
            {
                add = false;
                toolStripButtonOk.Visible = false;
                toolStripButtonAdd.Visible = true;
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            String manv = txtMaNV.Text;
            if (manv.Length != 0)
            {
                KhachHangDAO KhachHangDAO = new KhachHangDAO();
                if (!KhachHangDAO.DeleteById(manv))
                {
                    MessageBox.Show("Không xóa được khách hàng", "Error Sytem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    DataSet ds = KhachHangDAO.GetAll();
                    dataGridViewKhachHang.DataSource = ds.Tables[0];
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        BinddingData(KhachHangDAO.GetById(dataGridViewKhachHang.Rows[0].Cells[0].Value.ToString()));
                    }
                }
            }
        }

        public KhachHang getData()
        {
            KhachHang khachHang = null;
            try
            {
                khachHang = new KhachHang();
                khachHang.Makh = txtMaNV.Text;
                khachHang.Holot = txtHoLot.Text;
                khachHang.Tenkh = txtTenNV.Text;
                khachHang.Sdt = txtSDT.Text;
                khachHang.Ngaysinh = dateTimePickerNgaySinh.Value;
                khachHang.Gioitinh = comboBoxGT.SelectedIndex == 0 ? true : false;
                khachHang.Socmnd = txtSCMND.Text;
                return khachHang;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            KhachHang KhachHang = getData();
            if (KhachHang != null)
            {
                KhachHangDAO KhachHangDAO = new KhachHangDAO();
                if (!KhachHangDAO.UpdateById(KhachHang))
                {
                    MessageBox.Show("Không cập nhật được khách hàng", "Error Sytem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    DataSet ds = KhachHangDAO.GetAll();
                    dataGridViewKhachHang.DataSource = ds.Tables[0];
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        BinddingData(KhachHangDAO.GetById(dataGridViewKhachHang.Rows[0].Cells[0].Value.ToString()));
                    }
                }
            }
        }

        private void UcKhachHang_Load(object sender, EventArgs e)
        {
            KhachHangDAO KhachHangDAO = new KhachHangDAO();
            DataSet ds = KhachHangDAO.GetAll();
            DataGridViewTextBoxColumn columnMaNV = new DataGridViewTextBoxColumn();
            columnMaNV.HeaderText = "Mã khách hàng";
            columnMaNV.DataPropertyName = "makh";
            dataGridViewKhachHang.Columns.Add(columnMaNV);
            DataGridViewTextBoxColumn columnHolot = new DataGridViewTextBoxColumn();
            columnHolot.HeaderText = "Họ lót";
            columnHolot.DataPropertyName = "holot";
            dataGridViewKhachHang.Columns.Add(columnHolot);
            DataGridViewTextBoxColumn columnTenNV = new DataGridViewTextBoxColumn();
            columnTenNV.HeaderText = "Tên khách hàng";
            columnTenNV.DataPropertyName = "tenkh";
            dataGridViewKhachHang.Columns.Add(columnTenNV);
            DataGridViewTextBoxColumn columnPhone = new DataGridViewTextBoxColumn();
            columnPhone.HeaderText = "Số điện thoại";
            columnPhone.DataPropertyName = "sdt";
            dataGridViewKhachHang.Columns.Add(columnPhone);
            DataGridViewTextBoxColumn columnNgaySinh = new DataGridViewTextBoxColumn();
            columnNgaySinh.HeaderText = "Ngày sinh";
            columnNgaySinh.DataPropertyName = "ngaysinh";
            dataGridViewKhachHang.Columns.Add(columnNgaySinh);
            DataGridViewTextBoxColumn columnGioiTinh = new DataGridViewTextBoxColumn();
            columnGioiTinh.HeaderText = "Giới tính";
            columnGioiTinh.DataPropertyName = "gioitinh";
            dataGridViewKhachHang.Columns.Add(columnGioiTinh);
            DataGridViewTextBoxColumn columnSoCMND = new DataGridViewTextBoxColumn();
            columnSoCMND.HeaderText = "Số cmnd";
            columnSoCMND.DataPropertyName = "socmnd";
            dataGridViewKhachHang.Columns.Add(columnSoCMND);
            dataGridViewKhachHang.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridViewKhachHang.DataSource = ds.Tables[0];
        }

        private void toolStripButtonOk_Click(object sender, EventArgs e)
        {
            if (add)
            {
                add = false;
                toolStripButtonOk.Visible = false;
                toolStripButtonAdd.Visible = true;
                KhachHang KhachHang = getData();
                if (KhachHang != null)
                {
                    KhachHangDAO KhachHangDAO = new KhachHangDAO();
                    if (!KhachHangDAO.Insert(KhachHang))
                    {
                        MessageBox.Show("Không tạo thêm được khách hàng", "Error Sytem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        DataSet ds = KhachHangDAO.GetAll();
                        dataGridViewKhachHang.DataSource = ds.Tables[0];
                        if (ds.Tables[0].Rows.Count != 0)
                        {
                            BinddingData(KhachHangDAO.GetById(dataGridViewKhachHang.Rows[0].Cells[0].Value.ToString()));
                        }
                    }
                }
            }
        }

        private void dataGridViewKhachHang_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
